package com.ship.common.redis.configure;

import com.ship.common.core.utils.Constants;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置
 *
 * @author zhangyh
 */
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport
{
//    private CacheManagerProperties cacheManagerProperties;
//
//
////    @Autowired
////    public void setCacheManagerProperties(CacheManagerProperties cacheManagerProperties) {
////        this.cacheManagerProperties = cacheManagerProperties;
////    }
//
//    @Bean(name = "cacheManager")
//    @Primary
//    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//        RedisCacheConfiguration difConf = getDefConf().entryTtl(Duration.ofHours(1));
//        int configSize = cacheManagerProperties.getCacheManages() == null ? 0 : cacheManagerProperties.getCacheManages().size();
//        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(configSize);
//        if (configSize > 0) {
//            cacheManagerProperties.getCacheManages().forEach(e -> {
//                RedisCacheConfiguration conf = getDefConf().entryTtl(Duration.ofSeconds(e.getSecond()));
//                redisCacheConfigurationMap.put(e.getKey(), conf);
//            });
//        }
//        return RedisCacheManager.builder(redisConnectionFactory)
//                .cacheDefaults(difConf)
//                .withInitialCacheConfigurations(redisCacheConfigurationMap)
//                .build();
//    }
//
//    @Bean
//    public RedisTemplate<String, Object> bxRedisTemplate(RedisConnectionFactory factory) {
//        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(factory);
//        redisTemplate.setKeySerializer(Constants.STRING_SERIALIZER);
//        redisTemplate.setHashKeySerializer(Constants.STRING_SERIALIZER);
//        redisTemplate.setValueSerializer(Constants.OBJECT_SERIALIZER);
//        redisTemplate.setHashValueSerializer(Constants.OBJECT_SERIALIZER);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public RedisRepository redisRepository(RedisTemplate<String, Object> bxRedisTemplate) {
//        return new RedisRepository(bxRedisTemplate);
//    }
//
//    private RedisCacheConfiguration getDefConf() {
//        return RedisCacheConfiguration.defaultCacheConfig()
//                .disableCachingNullValues()
//                .computePrefixWith(cacheName -> "cache".concat(":").concat(cacheName).concat(":"))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer()));
//    }
    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();
        return template;
    }
}
